Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
keep original array order when appling a active record "where" on it

Just a question, i have an array with some ids array = [19, 5, 1, 4] when i call a where on it Delivery.where(id: array.map(&:id)) to have an active record relation instead an array, the “where” statement is sorting the ids and give me all objects sorting by ids : #<ActiveRecord::Relation [#<Delivery id: 1, ... >, #<Delivery id: 4, ... >, #<Delivery id: 5, ... >, #<Delivery id: 19, ... > Is anyone know how to keep the original order ?

Thanks !

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

ActiveRecord.find – when called with an array – will return the records in exactly the same order automatically.

Just change your query to:

Delivery.find(array.map(&:id))

Quote from the docs:

NOTE: The returned records are in the same order as the ids you provide. [...]

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda